package com.estore.view;

import com.estore.model.Product;
import com.estore.service.ProductService;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;

public class ConsoleShop {
    private final ProductService productService = new ProductService();
    private final Map<String, Integer> shoppingCart = new HashMap();
    private final Scanner scanner;

    public ConsoleShop() {
        this.scanner = new Scanner(System.in);
    }

    public void start() {
        while(true) {
            this.showMenu();
            int choice = this.scanner.nextInt();
            this.scanner.nextLine();
            switch (choice) {
                case 1:
                    this.browseProducts();
                    break;
                case 2:
                    this.addToCart();
                    break;
                case 3:
                    this.viewCart();
                    break;
                case 4:
                    productService.saveData(); // 退出前保存数据
                    System.out.println("感谢使用，再见！");
                    break;
                case 99: // 隐藏的备份选项
                    productService.backupData();
                    return;
                default:
                    System.out.println("无效的选项，请重新输入！");
            }
        }
    }

    private void showMenu() {
        System.out.println("\n ----欢迎来到商城---- ");
        System.out.println("1. 浏览所有商品");
        getPrintln();
        System.out.println("3. 查看购物车");
        System.out.println("4. 退出");
        System.out.print("请输入您的选择: ");
        System.out.println("test");
    }

    private static void getPrintln() {
        System.out.println("2. 添加商品到购物车");
    }

    private void browseProducts() {
        System.out.println("\n--- 商品列表 ---");
        List<Product> products = this.productService.getAllProducts();
        if (products.isEmpty()) {
            System.out.println("商店里还没有商品。");
        } else {
            PrintStream var10001 = System.out;
            Objects.requireNonNull(var10001);
            products.forEach(var10001::println);
        }

    }

    private void addToCart() {
        System.out.print("请输入要添加的商品ID: ");
        String productId = this.scanner.nextLine();
        Product product = this.productService.findProductById(productId);
        if (product == null) {
            System.out.println("错误：找不到该商品！");
        } else if (product.getStock() <= 0) {
            System.out.println("抱歉，该商品已售罄。");
        } else {
            System.out.print("请输入购买数量: ");
            int quantity = this.scanner.nextInt();
            this.scanner.nextLine();
            if (quantity <= 0) {
                System.out.println("错误：购买数量必须为正数！");
            } else if (quantity > product.getStock()) {
                System.out.println("错误：库存不足！当前库存为 " + product.getStock());
            } else {
                this.shoppingCart.put(productId, (Integer)this.shoppingCart.getOrDefault(productId, 0) + quantity);
                System.out.println("成功将 '" + product.getName() + "' 添加到购物车！");
            }
        }
    }

    private void viewCart() {
        System.out.println("\n--- 我的购物车 ---");
        if (this.shoppingCart.isEmpty()) {
            System.out.println("购物车是空的，快去逛逛吧！");
        } else {
            double total = (double)0.0F;

            for(Map.Entry<String, Integer> entry : this.shoppingCart.entrySet()) {
                String productId = (String)entry.getKey();
                Integer quantity = (Integer)entry.getValue();
                Product product = this.productService.findProductById(productId);
                if (product != null) {
                    double subtotal = product.getPrice() * (double)quantity;
                    System.out.printf("商品: %-20s | 单价: ¥%-8.2f | 数量: %-3d | 小计: ¥%.2f\n", product.getName(), product.getPrice(), quantity, subtotal);
                    total += subtotal;
                }
            }

            System.out.println("------------------------------------");
            System.out.printf("总计: ¥%.2f\n", total);
        }
    }

    public static void main(String[] args) {
        new ConsoleShop().start();
    }
}